<?php

namespace App\Jobs;

use App\Entity\UserEntity;
use App\Services\Distribution\Poster;
use App\VO\Product\MetaInfo\ProductInfo;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class DistributionPosterProductJob implements ShouldQueue
{
    use Dispatchable; use InteractsWithQueue; use Queueable; use SerializesModels;

    /**
     * @var int
     */
    public $city_id;
    /**
     * @var UserEntity
     */
    public $user;
    /**
     * @var ProductInfo
     */
    public $productInfo;

    /**
     * Create a new event instance.
     *
     * @param int $city_id
     * @param UserEntity $user
     * @param ProductInfo $productInfo
     * @return void
     */
    public function __construct(int $city_id, UserEntity $user, ProductInfo $productInfo)
    {
        $this->city_id = $city_id;
        $this->user = $user;
        $this->productInfo = $productInfo;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $poster = new Poster($this->user);
            $poster->makeProduct($this->city_id, $this->productInfo);
        } catch (\Throwable $e) {
            logException($e);
        }
    }
}
